package com.zmc.coupon.controller;

import java.util.List;

import com.zmc.common.annotation.PreventRepeatSubmit;
import com.zmc.common.domain.query.coupon.CouponEsFeignQuery;
import com.zmc.common.domain.query.coupon.CouponFeignQuery;
import com.zmc.common.domain.vo.coupon.CouponReduceVo;
import com.zmc.common.domain.vo.coupon.CouponUpdateVo;
import com.zmc.common.enums.RequestSourceEnum;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.coupon.application.CouponApplicationService;
import com.zmc.coupon.enums.CouponEnum;
import com.zmc.coupon.infrastructure.bean.query.CouponQuery;
import com.zmc.coupon.infrastructure.bean.vo.CouponReceiveVo;
import com.zmc.coupon.infrastructure.bean.vo.CouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zmc.coupon.infrastructure.entity.CouponEntity;
import com.zmc.coupon.infrastructure.service.CouponService;
import com.zmc.common.utils.R;

import javax.servlet.http.HttpServletResponse;



/**
 * 优惠券信息
 *
 * @author zmc
 * @email 
 * @date 2024-10-09 14:59:36
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @Autowired
    private CouponApplicationService couponApplicationService;

    @Autowired
    private AuthTokenService authTokenService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public R list(@RequestBody CouponQuery query){
        return R.page(couponService.queryPage(query));
    }

    /**
     * 导出列表
     */
    @PostMapping("/export")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public void export(HttpServletResponse response, CouponQuery query) {
        List<CouponEntity> list = couponService.queryList(query);
        ExcelUtil<CouponEntity> util = new ExcelUtil<>(CouponEntity.class);
        util.exportExcel(response, list, "优惠卷数据");
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody CouponVo vo){
        return R.ok(couponService.getById(vo.getId()));
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R save(@RequestBody CouponEntity coupon){
        coupon.setUseCount(0);
        coupon.setReceiveCount(0);
        //默认不限等级
        coupon.setMemberLevel(CouponEnum.COUPON_MEMBER_LEVEL_ALL.getCode());
        //默认购物赠卷
        coupon.setCouponType(CouponEnum.COUPON_TYPE_SHOPPING.getCode());
        //默认指定商品
        coupon.setUseType(CouponEnum.COUPON_USE_TYPE_GOODS.getCode());
        //默认未发布
        coupon.setPublish(CouponEnum.COUPON_IS_PUBLISH.getCode());
        coupon.setMerchantId(authTokenService.getUserInfo().getMerchantId());
		couponService.save(coupon);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R update(@RequestBody CouponEntity coupon){
		couponService.updateById(coupon);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R remove(@RequestBody CouponVo vo) {
        couponApplicationService.removeById(vo.getId());
        return R.ok();
    }

    /**
     *
     * 获取商品优惠卷、满减、以及阶梯价格等优惠活动
     */
    @PostMapping("/detail")
    public R getCouponDetail(@RequestBody CouponFeignQuery query) {
        return couponApplicationService.getCouponDetail(query);
    }

    /**
     * 领取优惠卷
     */
    @PostMapping("/receive")
    public R receiveCoupon(@RequestBody CouponReceiveVo vo) {
        return R.ok(couponApplicationService.receiveCoupon(vo));
    }

    /**
     * 使用优惠卷
     */
    @PostMapping("/reduce")
    public R couponReduce(@RequestBody CouponReduceVo vo) {
        couponApplicationService.reduceCoupon(vo);
        return R.ok();
    }

    /**
     * 分单完成, 同步优惠卷orderSn（由总订单更新至分单后订单）
     * */
    @PostMapping("/update/history")
    public R updateCouponHistory(@RequestBody CouponUpdateVo vo) {
        couponApplicationService.updateCouponHistory(vo);
        return R.ok();
    }

    /**
     * 展示商品列表（注：当前优惠卷下过滤已选择商品 必须有阈值，否则es性能有严重影响）
     */
    @PostMapping("/good/list")
    public R goodList(@RequestBody CouponEsFeignQuery query) {
        return R.page(couponApplicationService.getGoodList(query));
    }

}
